export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}
export function scorePassword (pass) {
  let score = 0
  if (!pass) {
    return score
  }
  // award every unique letter until 5 repetitions
  const letters = {}
  for (let i = 0; i < pass.length; i++) {
      letters[pass[i]] = (letters[pass[i]] || 0) + 1
      score += 5.0 / letters[pass[i]]
  }

  // bonus points for mixing it up
  const variations = {
      digits: /\d/.test(pass),
      lower: /[a-z]/.test(pass),
      upper: /[A-Z]/.test(pass),
      nonWords: /\W/.test(pass)
  }

  let variationCount = 0
  for (var check in variations) {
      variationCount += (variations[check] === true) ? 1 : 0
  }
  score += (variationCount - 1) * 10

  return parseInt(score)
}

/**
 * 校验密码强度---沒有匹配到以下級別就提示
 * @param {*} value
 * @returns
 */
export function checkPassWord(value){
  // 0： 表示第一个级别 1：表示第二个级别 2：表示第三个级别
  // 3： 表示第四个级别 4：表示第五个级别
  const strength = {
    0: '很弱',
    1: '较弱',
    2: '强',
    3: '很强',
    // 4: ''
  }
  const arr = []; const array = [1,2,3,4]
  if (value.length < 6){ // 最初级别
      return 0
  }
  if (/\d/.test(value)){ // 如果用户输入的密码 包含了数字
      arr.push(1)
  }
  if (/[a-z]/.test(value)){ // 如果用户输入的密码 包含了小写的a到z
      arr.push(2)
  }
  if (/[A-Z]/.test(value)){ // 如果用户输入的密码 包含了大写的A到Z
      arr.push(3)
  }
  // if (/\W/.test(value)){ // 如果是非数字 字母 下划线
  //     arr.push(4)
  // }
  for (let i = 0; i < array.length; i++){
      if (arr.indexOf(array[i]) == -1){
          return strength[array[i]]
      }
  }
}

/**
 * 循环遍历树的每一个元素
 * @param {*} tree
 * @param {*} callBack
 */
export function foreachTree(tree, callBack = () => {}) {
  tree.forEach(element => {
    callBack(element)
    if (element.children) {
      foreachTree(element.children, callBack)
    }
  })
}

